<template>
  <el-card shadow="hover">
    <div class="sick">
      <el-card shadow="never">
        <div class="my-card">
          <div class="condition">
            <el-form ref="form" :model="form" label-width="80px">
              <el-form-item>
                <el-input placeholder="请输入医院名称" v-model="input" class="input-with-select">
                  <el-button slot="append" icon="el-icon-search" @click="doFindLike"></el-button>
                </el-input>
              </el-form-item>
            </el-form>
          </div>
          <div class="btn-event">
            <el-button @click="addUI" type="primary" class="left">添加</el-button>
            <el-button @click="importxls" class="left" type="success" icon="el-icon-upload2">导入</el-button>
            <el-button @click="exportxls" class="left" type="warning" icon="el-icon-download">导出</el-button>
          </div>
        </div>
      </el-card>
      <el-table :data="tableData"  height="480" style="width: 100%; margin-bottom: 20px" row-key="permissionId" border>
        <el-table-column prop="bedId" label="床位编号" sortable></el-table-column>
        <el-table-column prop="userId" label="拥有该床位的股东/经销商" sortable></el-table-column>
        <el-table-column prop="bedStatus" label="床位状态" sortable></el-table-column>
        <el-table-column prop="homeName" label="所处病房" sortable></el-table-column>

        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <!-- <el-button type="primary" >详情</el-button> -->
            <el-button type="info" @click="updateUI(scope.row)" plain size="mini">修改
            </el-button>
            <el-button type="danger" @click="del(scope.row)" plain size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="病房" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="所在医院：" :label-width="formLabelWidth">
            <el-select v-model="form.hId" placeholder="请选择医院" @change="dofindAllDept">
              <el-option v-for="item in options" :key="item.hId" :label="item.hName" :value="item.hId">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="所在科室：" :label-width="formLabelWidth">
            <el-select v-model="form.deptId" placeholder="请选择科室" @change="findAllHome">
              <el-option v-for="item in options1" :key="item.deptId" :label="item.deptName" :value="item.deptId">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="所在病房：" :label-width="formLabelWidth">
            <el-select v-model="form.homeId" placeholder="请选择病房">
              <el-option v-for="item in options2" :key="item.homeId" :label="item.homeName" :value="item.homeId">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="股东/经销商：" :label-width="formLabelWidth">
            <el-select v-model="form.userId" placeholder="请选择股东/经销商">
              <el-option v-for="item in options3" :key="item.userId" :label="item.userName" :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="Excel数据导入" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-upload class="upload-demo" action="http://localhost:8080/sickroom/import" :on-success="onSuccess" drag>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传xlsx文件</div>
        </el-upload>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="uploadxls">确 定</el-button>
        </span>
      </el-dialog>
      <el-pagination @size-change="handleSizeChange" background @current-change="handleCurrentChange"
        :current-page="pageInfo.page" :page-sizes="[5, 10, 15, 20]" :page-size="pageInfo.size"
        layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total">
      </el-pagination>
    </div>
  </el-card>
</template>

<script>

import { findAll, doSaveOrUpdate, del, dofindAllHospital, dofindAllDept, dofindAllHome, dofindAllShareholder } from "@/api/bed";
import { findAllHospital, findAllDept } from "@/api/sickroom"

export default {
  mounted() {
    this.updateTable();
  },
  data() {
    return {
      form: {},
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '180px',
      dialogVisible: false,
      input: '',
      fileList: [],
      pageInfo: {
        total: 0,
        page: 1,
        size: 5,
      },
      options: [],
      options1: [],
      options2: [],
      options3: [],
    }
  },
  methods: {
    onSuccess() {
      this.updateTable();
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done();
        })
        .catch((_) => { });
    },
    saveOrUpdate() {
      doSaveOrUpdate(this.form).then((r) => {
        if (r.code == 200) {
          this.dialogFormVisible = false;
          this.$message.success(r.msg);
          this.updateTable();
        }
      });
    },
    dofindAllDept() {
      findAllDept(this.form.hId).then(r => {
        this.options1 = r.data;
      })
    },
    findAllHome() {
      let deptId = this.form.deptId;
      let hId = this.form.hId;
      let location = {
        hId,
        deptId
      }
      dofindAllHome(location).then(r => {
        this.options2 = r.data;
      })

    },
    handleSizeChange(size) {
      this.pageInfo.size = size;
      this.updateTable();
    },
    handleCurrentChange(page) {
      this.pageInfo.page = page;
      this.updateTable();
    },
    addUI() {
      (this.options = []), (this.options1 = []), (this.form = {});
      this.dialogFormVisible = true;
      findAllHospital().then((r) => {
        this.options = r.data;
      });
      dofindAllShareholder().then(r => {
        this.options3 = r.data;
      })
    },
    updateUI(row) {
      findAllHospital().then((r) => {
        this.options = r.data;
      });
      dofindAllShareholder().then(r => {
        this.options3 = r.data;
      })
      this.dofindAllDept(row.hId);
      this.dialogFormVisible = true;
      this.form = row;
    },
    updateTable() {
      findAll(this.pageInfo).then((r) => {
        this.tableData = r.data.list;
        this.pageInfo.total = r.data.total;
      });
    },
    del() {

    },
    importxls() {

    },
    exportxls() {

    },
    uploadxls() {

    },
    doFindLike() {

    }
  }

}
</script>

<style scoped>
.el-card {
  margin-bottom: 15px;
}

.sick .my-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.condition {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-input {
  margin-left: 20px;
}

.btn-event {
  margin-right: 80px;
}

.el-pagination {
  text-align: left;
}
</style>